package bridge

import (
	"context"
	"errors"
	"fmt"
)

type NotifyInfoBuilder struct {
	n *NotifyInfo
}

func NewNotifyInfoBuilder() *NotifyInfoBuilder {
	return &NotifyInfoBuilder{n: &NotifyInfo{}}
}

func (b *NotifyInfoBuilder) SetUserName(userName string) *NotifyInfoBuilder {
	b.n.UserName = userName
	return b
}
func (b *NotifyInfoBuilder) SetPhone(phone string) *NotifyInfoBuilder {
	b.n.Phone = phone
	return b
}
func (b *NotifyInfoBuilder) SetEmail(email string) *NotifyInfoBuilder {
	b.n.Email = email
	return b
}
func (b *NotifyInfoBuilder) SetWechatID(wechatID string) *NotifyInfoBuilder {
	b.n.WechatID = wechatID
	return b
}
func (b *NotifyInfoBuilder) SetWechatGroupID(wechatGroupID string) *NotifyInfoBuilder {
	b.n.WechatGroupID = wechatGroupID
	return b
}
func (b *NotifyInfoBuilder) SetMessage(message string) *NotifyInfoBuilder {
	b.n.Message = message
	return b
}

func (b *NotifyInfoBuilder) Build() (*NotifyInfo, error) {
	if b.n.UserName == "" {
		return nil, errors.New("username is required")
	}
	if b.n.Message == "" {
		return nil, errors.New("message is required")
	}

	return b.n, nil
}

type NotifyInfo struct {
	UserName      string
	Message       string
	Phone         string
	Email         string
	WechatID      string
	WechatGroupID string
}

type NotifyInterface interface {
	Notify(ctx context.Context, info NotifyInfo) error
}

type PhoneNotify struct {
}

func NewPhoneNotify() *PhoneNotify {
	return &PhoneNotify{}
}

func (n *PhoneNotify) Notify(ctx context.Context, info NotifyInfo) error {
	if info.Phone == "" {
		return errors.New("phone is required")
	}
	return n.callPhoneNumber(ctx, info.Phone)
}
func (n *PhoneNotify) callPhoneNumber(ctx context.Context, phone string) error {
	fmt.Printf("拨打电话:%s成功...\n", phone)
	return nil
}

type EmailNotify struct {
}

func NewEmailNotify() *EmailNotify {
	return &EmailNotify{}
}

func (n *EmailNotify) Notify(ctx context.Context, info NotifyInfo) error {
	if info.Email == "" {
		return errors.New("email is required")
	}
	return n.sendEmail(ctx, info.Email)
}
func (n *EmailNotify) sendEmail(ctx context.Context, email string) error {
	fmt.Printf("发送邮件:%s成功...\n", email)
	return nil
}

type MessageNotify struct {
}

func NewMessageNotify() *MessageNotify {
	return &MessageNotify{}
}

func (n *MessageNotify) Notify(ctx context.Context, info NotifyInfo) error {
	if info.Phone == "" {
		return errors.New("phone is required")
	}
	return n.sendMessage(ctx, info.Phone)
}
func (n *MessageNotify) sendMessage(ctx context.Context, phone string) error {
	fmt.Printf("发送短信:%s成功...\n", phone)
	return nil
}

type WechatNotify struct {
}

func NewWechatNotify() *WechatNotify {
	return &WechatNotify{}
}

func (n *WechatNotify) Notify(ctx context.Context, info NotifyInfo) error {
	if info.WechatID == "" {
		return errors.New("wechatID is required")
	}
	return n.sendWechat(ctx, info.Phone)
}
func (n *WechatNotify) sendWechat(ctx context.Context, wechatID string) error {
	fmt.Printf("发送微信:%s成功...\n", wechatID)
	return nil
}

type WechatGroupNotify struct {
}

func NewWechatGroupNotify() *WechatGroupNotify {
	return &WechatGroupNotify{}
}

func (n *WechatGroupNotify) Notify(ctx context.Context, info NotifyInfo) error {
	if info.WechatGroupID == "" {
		return errors.New("wechatGroupID is required")
	}
	return n.sendWechatGroup(ctx, info.WechatGroupID)
}
func (n *WechatGroupNotify) sendWechatGroup(ctx context.Context, wechatGroupID string) error {
	fmt.Printf("发送微信群:%s成功...\n", wechatGroupID)
	return nil
}
